layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 调度单表管理
     */
    var TransportDispatch = {
        tableId: "transportDispatchTable"
    };

    /**
     * 初始化表格的列
     */
    TransportDispatch.initColumn = function () {
        return [[
            {field: 'customerOrder', sort: true, title: '客户单号', style:'background-color: #dbf1e1;font-weight: 600;',width: 210},
            {field: 'setMentiontime', sort: true, title: '发货日期', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'lineName', sort: true, title: '线路', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'getUnit', sort: true, title: '收货单位', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'getLocation', sort: true, title: '收货地址', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'kilometer', sort: true, title: '公里数', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'goodsName', sort: true, title: '商品名称', style:'background-color: #dbf1e1;font-weight: 600;', totalRowText: '合计：'},
            {field: 'goodsNumber', sort: true, title: '编号', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'goodsNo', sort: true, title: '货号', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'batchnumber', sort: true, title: '批次'},
            {field: 'transportMoneyD', sort: true, title: '金额', totalRow: true},
            {field: 'transportPutvolume', sort: true, title: '体积', totalRow: true, style:'font-weight: 600;'},
            {field: 'num', sort: true, title: '数量', totalRow: true, style:'font-weight: 600;'},
            {field: 'tooDeployVolume', sort: true, title: '配置体积', totalRow: true, style:'font-weight: 600;'},
            {field: 'tooDeployNum', sort: true, title: '配置数', totalRow: true, style:'font-weight: 600;color: #2b85e4;text-decoration:underline;',edit:'text'},
        ]];
    };

    TransportDispatch.search = function () {
        var queryData = {};
        queryData['customerOrder'] = $("#customerOrder").val();
        queryData['goodsName'] = $("#goodsName").val();
        table.reload(TransportDispatch.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    // Render table
    var tableResult = table.render({
        elem: '#' + TransportDispatch.tableId,
        url: Feng.ctxPath + '/orderTransportDispatch/listOrder2?dispatchNumber='+Feng.getUrlParam("dispatchNumber"),
        page: false,
        limit: 1000,
        height: "full-60",
        totalRow: true,
        toolbar: '#toolbarDemo', //Enable header toolbar and bind left template
        cellMinWidth: 100,
        cols: TransportDispatch.initColumn()
    });

    // Edit cell event
    table.on('edit(' + TransportDispatch.tableId + ')', function (obj) {
        var data = obj.data;//Get all key-value pairs in the row
		var value = obj.value;//Modified value
		if(value == ''){
			table.reload('transportDispatchTable')	
    		layer.msg('Please enter a valid number'); 
			return
		}
		if(typeof parseFloat(value) != 'number' ||  parseFloat(value) <= 0){
			table.reload('transportDispatchTable')	
    		layer.msg('Please enter a valid positive number'); 
			return
		}
		if(parseFloat(value) > parseFloat(data.num)){
			table.reload('transportDispatchTable')	
    		layer.msg('Exceeding order quantity'); 
			return
		}
		
        var ajax = new $ax(Feng.ctxPath + "/orderTransportDispatch/tiaozhengGoodsDispatch?id="+data.id+"&tooDeployNum="+value+"&dispatchNumber="+data.dispatchNumber+"&carId="+data.carId, function (data) {
    		if (data.success) {
                Feng.success("Update successful!");
				parent.submit = 1
        		table.reload(TransportDispatch.tableId);
			} else {
                Feng.error("Update failed！"+data.message);
			}
        }, function (data) {
            Feng.error("Update failed!" + data.responseJSON.message + "!");
        });
        ajax.start();
    });

    // Search button click event
    $('#btnSearch').click(function () {
        TransportDispatch.search();
    });

});
